local Class = require("Core.Class")
local ResManager = require('Game.Manager.ResManager')
local UIManager = require('Game.Manager.UIManager')
local EventManager = require('Game.Manager.EventManager')
local LoadLevelManager = require('Game.Manager.LoadLevelManager')
local Coroutine = require("Core.Coroutine")
local UnityUtils = require("Game.Util.UnityUtils")
local CoroutineManager = CS.GameCore.CoroutineManager.Instance

---@class GameManager
local GameManager = Class("GameManager")

function GameManager:__init()
    -- 常数

    -- 管理的属性
    self.emenyMaxCount = 5
    self.monsterBirthInterval = 2

    -- 当前属性/状态
    self.currentLevelIndex = 0
    self.currentGameMode = 0
    self.currentEmenyCount = 0

    -- 添加游戏事件
    self:AddListeners()

    -- 游戏开始
    self:GameStart()
end

function GameManager:GameStart()
    -- 开启游戏起始场景
    ResManager.LoadScene('Game', function ()
        UIManager:ShowPanel
        (
            'StartPanel',
            function (panel)
                LoadLevelManager:OnInit()
            end,
            function (errorInof)
                CSE.Debug.LogError(errorInof)
            end
        )
    end)
end

function GameManager:AddListeners()
    self:AddPlayGameEvent()
    self:AddLoadingLevelEndEvent()
    self:AddGameOverEvent()
end

function GameManager:EmenyBirth()
    -- 停止上一个怪物诞生协程
    if self.EmenyBirthCoroutine ~= nil then
        CoroutineManager:StopCoroutine(self.EmenyBirthCoroutine)
        self.EmenyBirthCoroutine = nil
    end
    -- 开启怪物诞生协程
    local onEmenyBirth = function ()
        local emenyTypeList = require('Game.LevelMapItemPositionTable.Level1_EmenyType')
        local emenyPointList = require('Game.LevelMapItemPositionTable.EmenyBirthPoint')
        for _, value in pairs(emenyPointList) do
            ResManager.LoadGameObject('Assets/GameRes/Prefabs/Emenies/'..emenyTypeList[1]..'.prefab', CSE.Vector3(value[1], value[2], 0), CSE.Quaternion.Euler(0, 0, 180), function (obj)
                    -- 初始化敌人
            end)

            self.currentEmenyCount = self.currentEmenyCount + 1
        end
        coroutine.yield(nil)

        local timer = 0
        while true do
            -- 间隔两秒才进行生成
            while timer < self.monsterBirthInterval do
                timer = timer + CSE.Time.deltaTime
                coroutine.yield(nil)
            end

            timer = 0
            while self.currentEmenyCount > self.emenyMaxCount do
                coroutine.yield(nil)
            end

            math.randomseed(os.time())
            local emenyTypeIndex = math.random(#emenyTypeList)
            local emenyPointIndex = math.random(3)
            ResManager.LoadGameObject('Assets/GameRes/Prefabs/Emenies/'..emenyTypeList[emenyTypeIndex]..'.prefab', CSE.Vector3(emenyPointList[emenyPointIndex][1], emenyPointList[emenyPointIndex][2], 0), CSE.Quaternion.Euler(0, 0, 180), function (obj)
                    -- 初始化敌人
            end)

            self.currentEmenyCount = self.currentEmenyCount + 1
        end
    end
    local autoEmenyBrathCoroutine = Coroutine.Create(bind(onEmenyBirth))
    CoroutineManager:StartCoroutine(autoEmenyBrathCoroutine)
end

------------ EventManager管理的事件 ------------

function GameManager:AddPlayGameEvent()
    self.onPlayEvent = function (selectGameMode)
        -- 设置当前游戏模式 (单人/双人)
        self.currentGameMode = selectGameMode
        -- 开启游戏加载面板
        UIManager:ShowPanel('LoadingPanel', function (loadingPanel)
            self.currentLevelIndex = 1
            local waitOneSecond = Coroutine.Create(bind(UnityUtils.WaitFormSecond), 1, function ()
                LoadLevelManager:LoadLevel(self.currentLevelIndex, self.currentGameMode, function ()
                    UIManager:ShowPanel('LevelPanel', function (levelPanel)
                        -- 关闭加载UI
                        loadingPanel.LuaClass:OnLoaded();
                    end,function (errorInfo)
                        CSE.Debug.LogError(errorInfo)
                    end)
                end)
            end)
            CoroutineManager:StartCoroutine(waitOneSecond)
        end,function (errorInfo)
            CSE.Debug.LogError(errorInfo)
        end)
    end
    EventManager:AddListener('OnPlayGame', self.onPlayEvent)
end

function GameManager:AddLoadingLevelEndEvent()
    self.onPlayEvent = function ()
        -- 触发玩家诞生事件
        EventManager:Fire('PlayerBirth', 1)
        if self.currentGameMode == 2 then
            EventManager:Fire('PlayerBirth', 2)
        end

        -- 怪物开始诞生
        self:EmenyBirth()
    end
    EventManager:AddListener('LoadingLevelEnd', self.onPlayEvent)
end

function GameManager:AddGameOverEvent()
    self.onGameOver = function ()
        -- 清空地图
        LoadLevelManager:DeleteMap()

        CoroutineManager:StopAllCoroutines()

        -- 还原Manager数据
        self.currentLevelIndex = 0
        self.currentGameMode = 0
        self.currentEmenyCount = 0

        -- 关闭游戏界面
        UIManager:HidePanel('LevelPanel')

        -- 打开开始界面
        UIManager:ShowPanel('StartPanel', function ()
            -- 什么也不做
        end, function (errorInof)
            CSE.Debug.LogError(errorInof)
        end)
    end
    EventManager:AddListener('GameOver', self.onGameOver)
end

return GameManager.New()